<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/main.css" />
  <script type="text/javascript" src="./lib/jquery-1.12.4.min.js"></script>
  <script type="text/javascript" src="./lib/jquery-ui.min.js"></script>
  <script type="text/javascript" src="./lib/jquery.mousewheel.js"></script>
  <script type="text/javascript" src="./lib/scroll.js"></script>
  <title>聊天机器人</title>
</head>

<body>
  <div class="wrap">
    <!-- 头部 Header 区域 -->
    <div class="header">
      <h3>小思同学</h3>
      <img src="img/person01.png" alt="icon" />
    </div>
    <!-- 中间 聊天内容区域 -->
    <div class="main">
      <ul class="talk_list" style="top: 0px">
        <li class="left_word">
          <img src="img/person01.png" />
          <span>你好</span>
        </li>
        <li class="right_word">
          <img src="img/person02.png" />
          <span>你好哦</span>
        </li>
      </ul>
      <!-- 右侧滚动条 -->
      <div class="drag_bar" style="display: none">
        <div class="drager ui-draggable ui-draggable-handle" style="display: none; height: 412.628px"></div>
      </div>
    </div>
    <!-- 底部 消息编辑区域 -->
    <div class="footer">
      <img src="img/person02.png" alt="icon" />
      <!-- 输入框 -->
      <input type="text" placeholder="说的什么吧..." class="input_txt" />
      <!-- 发送按钮 -->
      <input type="button" value="发 送" class="input_sub" />
    </div>
  </div>

  <!-- 导入axios -->
  <script src="./lib/axios.js"></script>
  <script>
    // 调用 resetui 即可滚到底部
    // resetui()

    // 接口地址： http://ajax-api.itheima.net/api/robot
    // 聊天
    // 步骤：
    //  1.给发送按钮绑定事件
    //  2.将自己输入的内容渲染到dom上
    //  3.通过axios发送ajax请求获得Ai的回复
    //  4.将Ai的回复内容渲染到dom上

    // 获取元素
    const text = document.querySelector('.input_txt')
    const btn = document.querySelector('.input_sub')
    const talkList = document.querySelector('.talk_list')


    btn.addEventListener('click', function () {
      const spoken = text.value.trim()
      if (!spoken) alert('你输入的不能为空')
      text.value = ''
      // 创建节点
      const myLi = document.createElement('li')
      // 修改节点名字
      myLi.className = 'right_word'
      // 让数据渲染在节点上
      myLi.innerHTML = `<img src="img/person02.png" />
          <span>${spoken}</span>`
      // 把新节点添加到父节点上
      talkList.appendChild(myLi)

      resetui()



      axios({
        url: 'http://ajax-api.itheima.net/api/robot',
        method: 'GET',
        params: { spoken },
      })
        .then(({ data: res }) => {
          const robotText = res.data.info.text

          const robotLi = document.createElement('li')
          robotLi.className = 'left_word'
          robotLi.innerHTML = `<img src="img/person02.png" />
          <span>${robotText}</span>`
          talkList.appendChild(robotLi)
          resetui()

        })

    })
    text.addEventListener('keyup', function (e) {
      // console.log(e);
      if (e.keyCode === 13) {
        btn.click()
      }
    });

  </script>
</body>

</html>